home *** CD-ROM | disk | FTP | other *** search
/ PC-X 1997 October / pcx14_9710.iso / swag / delphi.swg / 0031_Loop Processing in Windows.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1995-11-22  |  2.3 KB  |  89 lines

  1.  
  2. Routine that allows Windows programs to continue to process while in a
  3. processing loop.
  4.  
  5.  
  6. There are two basic ways of handling loop processing in Windows
  7. that would otherwise take away control (tie up processing of your
  8. code and not allow other processes to run) of other running
  9. Window's applications.  The first and probably the most flexible
  10. approach is to write a processing loop and 'YIELD' to other
  11. Windows application's request.  The second approach would be to
  12. override TApplication's MessageLoop method and within the
  13. MessageLoop call your process.
  14.  
  15.  
  16. 'YIELD'
  17. If your procedure that takes a while to execute contains some
  18. sort of outer loop, make a call to a procedure that allows other
  19. apps to run.  Your time consuming code might look something like:
  20.  
  21.      ...
  22.      While MoreToDo do
  23.      begin
  24.        YieldToOthers;
  25.        DoSomeProcessing;
  26.      end;
  27.  
  28. A few notes concerning this procedure:
  29.  
  30.   *  The call to HALT may need to be modified to do any cleanup
  31.      required
  32.      (i.e.: close open files, etc.).
  33.  
  34.  
  35.   *  You'll need to be sensitive to re-entrant issues.
  36.  
  37. The procedure "YieldToOthers" would look like:
  38.  
  39.   Procedure YieldToOthers;
  40.   var
  41.     Msg : TMsg;
  42.   begin
  43.     While PeekMessage(Msg,0,0,0,PM_REMOVE) do begin
  44.       if Msg.Message = WM_QUIT then begin
  45.         Application^.Done;
  46.         halt; {!!}
  47.       end;
  48.       TranslateMessage(Msg);
  49.       DispatchMessage(Msg);
  50.     end;
  51.   end;
  52.  
  53. 'MessageLoop'
  54. This is a replacement for the TApplication MessageLoop method.
  55.  
  56. It provides for an 'idle loop' where your program can
  57. continuously process its own work, and yet still yield control to
  58. windows when windows needs to do something.
  59.  
  60.   TMyApp = object(TApplication)
  61.     procedure MessageLoop; virtual;  { add this method to your }
  62.   end;      {*  descendant of TApplication  *}
  63.  
  64.   {*  just paste this in to your program  *}
  65.   procedure TMyApp.MessageLoop;
  66.   var
  67.     Message: TMsg;
  68.   begin
  69.  
  70.     while true do begin
  71.       if PeekMessage(Message, 0, 0, 0, pm_Remove) then begin
  72.  
  73.         if Message.Message = wm_Quit then Exit;
  74.         TranslateMessage(Message);
  75.         DispatchMessage(Message);
  76.       end
  77.       else begin
  78.  
  79.         {**  do your stuff here  **}
  80.  
  81.       end;
  82.     end;
  83.  
  84.     Status := Message.WParam;
  85.  
  86.   end;
  87.  
  88.  
  89.